<?php

namespace app\admin\model;

use app\model\Model;

class Index extends Model
{
  public $db;

  function __construct()
  {
    include 'check.php';
    new Check();

    $this->db = $GLOBALS['db'];
  }

  function index()
  {
    $this->tpl('welcome', [
      'db' => $this->db
    ]);
  }

  function reset_admin_psw()
  {
    $psw = psw_hash('admin');
    $sql = "UPDATE user SET u_psw = '$psw' WHERE id = 1";
    $this->db->query($sql);
    echo $psw;
  }

  function login()
  {
    $this->tpl('login', [
      'db' => $this->db
    ]);
  }

  function login_action()
  {
    times_limit('login_admin_count', 'times_admin'); // 次数限制

    $vercode = str_safe($_POST['vercode']);
    if (!\check($vercode, 'verifycode', 'verifycode_admin')) {
      alert_href('验证码错误', M('admin', 'index', 'login'));
    }

    $a_name = str_safe($_POST['a_name']);
    $a_password = str_safe($_POST['a_password']);

    if ($res = $GLOBALS['db']->getRow("SELECT id,u_name,u_psw,u_enable FROM user WHERE u_name = '$a_name' AND u_enable = 1 AND u_isadmin = 1")) {

      if (!$res['u_enable']) {
        alert_href('管理员账号已失效', M('admin', 'index', 'login'));
      }

      if (psw_verify($a_password, $res['u_psw'])) {

        set_admin_token($res['id'], $res['u_name']); // 设置用户标示
        admin_log('管理员登陆 [id:' . $res['id'] . ']');
        unset($_SESSION['login_admin_count']);
        unset($_SESSION['times_admin']);

        href(M('admin', 'index', 'index'));
      }
      alert_href('用户名或密码错误', M('admin', 'index', 'login'));
    }
    alert_href('用户名或密码错误', M('admin', 'index', 'login'));
  }

  function logout()
  {
    admin_logout();
  }

  function baidupush()
  {
    set_time_limit(120);
    // 生成数组数据
    $y = date("Y");
    $m = date("m");
    $d = date("d");
    $days = mktime(0, 0, 0, $m, $d, $y);
    $daye = mktime(23, 59, 59, $m, $d, $y);
    $this->getDetailBaiduSend($days, $daye);
  }

  function clearupload()
  {
    $this->clearUploadfile();
  }

  function getDetailBaiduSend($s, $e)
  {
    $res = $GLOBALS['db']->getAll("SELECT id FROM detail WHERE d_date < " . $e . " AND d_date >= " . $s);
    if (!empty($res)) {
      $urls = array();
      foreach ($res as $val) {
        $urls[] = $GLOBALS['cms']['domain'] . '/' . d_url($val['id']) . ',';
      }
      // 百度推送
      $api = BAIDU_API;
      $ch = curl_init();
      $options =  array(
        CURLOPT_URL => $api,
        CURLOPT_POST => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POSTFIELDS => implode("\n", $urls),
        CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
      );
      curl_setopt_array($ch, $options);
      $result = curl_exec($ch);
      $j = json_decode($result, true);
      if (!empty($j['error'])) {
        alert_back('error:' . $j['error'] . ', message:' . $j['message']);
      }
      alert_back('剩余:' . $j['remain'] . '条，成功:' . $j['success'] . '条');
    }

    alert_back('无新数据无需推送');
  }

  function clearUploadfile()
  {
    $tbls = array(
      "image" => array(
        "channel" => "SELECT COUNT(*) FROM channel WHERE INSTR(c_picture, '{file_name}') OR INSTR(c_content, '{file_name}') OR INSTR(c_scontent, '{file_name}') OR INSTR(c_cover, '{file_name}') OR INSTR(c_slideshow, '{file_name}')",
        "chip" => "SELECT COUNT(*) FROM chip WHERE INSTR(c_content, '{file_name}')",
        "detail" => "SELECT COUNT(*) FROM detail WHERE (INSTR(d_picture, '{file_name}') OR INSTR(d_content, '{file_name}') OR INSTR(d_scontent, '{file_name}') OR INSTR(d_slideshow, '{file_name}')) AND d_date >= {log_dir}",
        "feedback" => "SELECT COUNT(*) FROM feedback WHERE (INSTR(f_content, '{file_name}') OR INSTR(f_answer, '{file_name}')) AND f_adate >= {log_dir}",
        "link" => "SELECT COUNT(*) FROM link WHERE INSTR(l_picture, '{file_name}')",
        "slideshow" => "SELECT COUNT(*) FROM slideshow WHERE INSTR(s_picture, '{file_name}')",
        "system" => "SELECT COUNT(*) FROM system WHERE INSTR(s_description, '{file_name}') OR INSTR(s_copyright, '{file_name}')"
      ),
      "file" => array(
        "channel" => "SELECT COUNT(*) FROM channel WHERE INSTR(c_content, '{file_name}') OR INSTR(c_scontent, '{file_name}')",
        "chip" => "SELECT COUNT(*) FROM chip WHERE INSTR(c_content, '{file_name}')",
        "detail" => "SELECT COUNT(*) FROM detail WHERE (INSTR(d_content, '{file_name}') OR INSTR(d_scontent, '{file_name}') OR INSTR(d_attachment, '{file_name}')) AND d_date >= {log_dir}",
        "feedback" => "SELECT COUNT(*) FROM feedback WHERE (INSTR(f_content, '{file_name}') OR INSTR(f_answer, '{file_name}')) AND f_adate >= {log_dir}",
        "system" => "SELECT COUNT(*) FROM system WHERE INSTR(s_description, '{file_name}') OR INSTR(s_copyright, '{file_name}')"
      )
    );

    // 获取log文件
    $log = ROOT_PATH . "uploadfile/clear_log.txt";
    if (is_file($log)) {
      $log_txt = file_get_contents($log);
      if (!empty($log_txt)) {
        list($log_dir, $log_date) = explode(' ', $log_txt);
      }
    } else {
      $log_dir = '0';
      $log_date = '0';
    }

    // 获取上传文件类型,目前只对image类型文件夹做清理
    $file_type = array("image", "file");

    // 获取类型目录下的日期文件夹
    foreach ($file_type as $key => $value) {
      $type = $value;
      $type_dir = ROOT_PATH . "uploadfile/" . $value;
      $arr = scandir($type_dir);
      foreach ($arr as $val) {
        if ($val >= $log_dir && $this->checkFile($val)) {
          $arr_file = scandir($type_dir . "/" . $val);
          foreach ($arr_file as $v) {
            if ($this->checkFile($v)) {
              $res[$value][$val][] = $v;
            }
          }
        }
        $log_dir = $val;
      }
      $this->clearFiles($tbls, $res, $type, $type_dir, $log_dir, $log);
    }

    alert_back("无用的上传文件清理完毕!");
  }

  function clearFiles($tbls, $res, $type, $type_dir, $log_dir, $log)
  {
    // 循环结果比对
    foreach ($res[$type] as $key => $arr) {
      $dir_date = mktime(0, 0, 0, $this->getDirMonth($key), $this->getDirDay($key), $this->getDirYear($key));
      foreach ($arr as $file_name) {
        $count = 0;
        foreach ($tbls[$type] as $tbl => $sql) {
          $sql = str_replace('{file_name}', $file_name, $sql);
          $sql = str_replace('{log_dir}', $log_dir, $sql);
          $count += $GLOBALS['db']->getOne($sql);
        }
        if ($count) {
          continue;
        }
        $f = "$type_dir/$file_name";
        // @unlink($f);
        file_delete($type_dir, $file_name);
        echo $f . '<BR>';
      }
    }

    // 写入log文件
    if (isset($type) && isset($log_dir)) {
      $file = fopen($log, "w") or die("无法创建文件!");
      fwrite($file, $log_dir . " " . date('Y-m-d H:i:s', gmtime()));
      fclose($file);
    }
  }

  function getDirYear($str)
  {
    return substr($str, 0, 4);
  }
  function getDirMonth($str)
  {
    return substr($str, 4, 2);
  }
  function getDirDay($str)
  {
    return substr($str, 6, 2);
  }
  function checkFile($str)
  {
    return $str != '.' && $str != '..' && $str != 'index.html' && $str != 'index.htm' ? true : false;
  }

  function __destruct()
  {
  }
}
